<template>
    <Teleport to="body">
        <transition name="el-fade-in">
            <div class="ticket-receive-dialog" v-if="visible">
                <div class="ticket-receive-dialog__mask"></div>
                <div class="ticket-receive-dialog__content">
                    <div class="ticket-receive-dialog__title">
                        {{ data?.user?.nickname }}分享的《{{ data?.works?.name }}》作品的门票
                    </div>

                    <div class="btn-receive" @click="onReceive()">点击领取门票</div>

                    <div class="btn-group">
                        <div class="btn-close" @click="onClose()">关闭</div>
                    </div>
                </div>
            </div>
        </transition>
    </Teleport>
</template>
<script setup>
import { GetTicketShareInfo, TicketReceiveShare } from "~/api";

const route = useRoute();
const share_code = ref(route.query['share_code']);
const visible = ref(false);
const data = ref({});
const userStore = useUserStore();

const onClose = () => {
    share_code.value = null;
    visible.value = false;
    data.value = {};
}

const onReceive = async () => {
    if (userStore.isLogin) {
        const res = await TicketReceiveShare(
            {
                share_code: share_code.value
            }
        );

        if (res.code == 200) {
            Message().success('领取成功');
            onClose();
        } else {
            Message().warning(res.msg);
        }
    } else {
        LoginModal(true, {
            url: location.href,
        }, undefined, undefined, () => {
            share_code.value = null;
        });
    }
}

const getInfo = async () => {
    const res = await GetTicketShareInfo({
        share_code: share_code.value
    });

    if (res.code == 200) {
        if (res.data) {
            data.value = res.data || {};
            visible.value = true;
        } else {
            visible.value = false;
            Message().warning('无效的分享链接');
        }
    }
}

if (share_code.value) {
    getInfo();
}

</script>
<style lang="scss" scoped>
.ticket-receive-dialog {
    position: fixed;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999;

    &__mask {
        position: relative;
        width: 100%;
        height: 100%;
        background-color: rgba($color: #000000, $alpha: 0.5);
    }

    &__title {
        font-size: 16px;
        padding-bottom: 20px;
        text-align: center;
    }

    &__content {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 350px;
        background-color: #fff;
        transform: translate(-50%, -50%);
        border-radius: 10px;
        padding: 10px 0;

        .btn-receive {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 120px;
            height: 34px;
            margin: 16px auto;
            font-size: 14px;
            background-color: #5085FB;
            color: #fff;
            border-radius: 4px;
            cursor: pointer;
        }


        .btn-group {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 40px;

            .btn-close {
                width: 60px;
                height: 32px;

                display: flex;
                align-items: center;
                justify-content: center;
                border: 1px solid #ccc;
                border-radius: 4px;
                font-size: 14px;
                cursor: pointer;
            }
        }
    }
}
</style>